package tool

import (
	"encoding/json"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type ComSnRule struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64      `json:"id" bson:"id,omitempty"`
	Name        string     `json:"name" bson:"name"`
	Code        string     `json:"code" bson:"code"`
	AttrChain   string     `json:"attrChain" bson:"attrChain"`
	CreateAt    time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *ComSnRule) DocName() string { return "ComSnRule" }
func (this *ComSnRule) GetId() int64    { return this.Id }
func (this *ComSnRule) SetId(id int64)  { this.Id = id }
func (this *ComSnRule) Create() error {
	return this.GetFactory().Create(this)
}
func (this *ComSnRule) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *ComSnRule) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *ComSnRule) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *ComSnRule) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *ComSnRule) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *ComSnRule) ToString() string {
	return string(this.ToBytes())
}
func (this *ComSnRule) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ComSnRule) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ComSnRule) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *ComSnRule) ClearRelationsBeforeSave() mgo.Doc {
	return this
}
func neverUsed_ComSnRule() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}
